﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;

using XMK.Framework.DataAccess;
using XMK.Tadong.Model;
using XMK.Utils;
using XMK.Framework.Model;
using XMK.Tadong.Model.Cond;
namespace XMK.Tadong.Data
{
   public  class EXTData
    {
       /// <summary>
        /// 数据库连接
        /// </summary>
        protected DbProvider dbProvider;

        public EXTData(DbProvider dbProvider)
        {
            this.dbProvider = dbProvider;
        }

        public ExtTypeinfo getExtCodeByExtent(string extent)
        {
            
                StringBuilder strSql = new StringBuilder();
                strSql.Append(@"select ext_type.* from ext_type join  ext_item on ext_type.ext_id=ext_item.ext_id where ext_item.item_code ='" + extent + "'");

                DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());
                cmd.DbCmdInfo.CommandType = CommandType.Text;
                ExtTypeinfo Obj = DbManager.GetInfo(cmd,
                    delegate(out ExtTypeinfo u, DataRow row)
                    {
                        u = new ExtTypeinfo
                        {
                            EXT_ID = ParseHelper.Parse<decimal>(row["EXT_ID"]),
                            EXT_CODE = ParseHelper.Parse<string>(row["EXT_CODE"]),
                            EXT_URL = ParseHelper.Parse<string>(row["EXT_URL"]),
                            PIC_NAME = ParseHelper.Parse<string>(row["PIC_NAME"])


                        };
                    });
                if (Obj != null)
                {
                    return Obj;
                }
                return null;
          
        }
        /// <summary>
        /// 根据格式值获取图标路径
        /// </summary>
        /// <param name="extent"></param>
        /// <returns></returns>
        public ExtTypeinfo getExtCodeByType(string fileType)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select * from EXT_TYPE where EXT_CODE ='" + fileType + "'");

            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            ExtTypeinfo Obj = DbManager.GetInfo(cmd,
                delegate(out ExtTypeinfo u, DataRow row)
                {
                    u = new ExtTypeinfo
                    {
                        EXT_ID = ParseHelper.Parse<decimal>(row["EXT_ID"]),
                        EXT_CODE = ParseHelper.Parse<string>(row["EXT_CODE"]),
                        EXT_URL = ParseHelper.Parse<string>(row["EXT_URL"]),
                        PIC_NAME = ParseHelper.Parse<string>(row["PIC_NAME"])


                    };
                });
            if (Obj != null)
            {
                return Obj;
            }
            return null;

        }

        /// <summary>
        /// 获取扩展名
        /// </summary>
        /// <param name="extent"></param>
        /// <returns></returns>
        public Base_ExtItemCond getItemByCode(string itemcode)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select * from EXT_ITEM where ITEM_CODE ='" + itemcode + "'");

            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            Base_ExtItemCond Obj = DbManager.GetInfo(cmd,
                delegate(out Base_ExtItemCond u, DataRow row)
                {
                    u = new Base_ExtItemCond
                    {
                        EXT_ID = ParseHelper.Parse<decimal>(row["EXT_ID"]),
                        ITEM_ID = ParseHelper.Parse<decimal>(row["ITEM_ID"]),
                        ITEM_CODE = ParseHelper.Parse<string>(row["ITEM_CODE"])
                       
                    };
                });
            if (Obj != null)
            {
                return Obj;
            }
            return null;

        }


        public ExtTypeinfo getExtCodeByExtId(decimal ext_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select * from ext_type where EXT_ID ="+ ext_id + "");

            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            ExtTypeinfo Obj = DbManager.GetInfo(cmd,
                delegate(out ExtTypeinfo u, DataRow row)
                {
                    u = new ExtTypeinfo
                    {
                        EXT_ID = ParseHelper.Parse<decimal>(row["EXT_ID"]),
                        EXT_CODE = ParseHelper.Parse<string>(row["EXT_CODE"]),
                        EXT_URL = ParseHelper.Parse<string>(row["EXT_URL"]),
                        PIC_NAME = ParseHelper.Parse<string>(row["PIC_NAME"])


                    };
                });
            if (Obj != null)
            {
                return Obj;
            }
            return null;

        }
        /// <summary>
        /// 类型列表
        /// </summary>
        /// <returns></returns>
        public List<ExtTypeinfo> QueryExtTypeList(PageInfo pageInfo, OrderInfo order)
        {
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, "sp_getpageExtTypelist",
                   delegate()
                   {
                       return DbCmdHelper.CreateParameters(dbProvider, true).ToArray();
                   });
            cmd.SetParamValues(order, pageInfo);
            List<ExtTypeinfo> exttypeList = DbManager.GetPageInfos<ExtTypeinfo>(cmd, pageInfo,
                delegate(out ExtTypeinfo dict, DataRow row)
                {
                    dict = new ExtTypeinfo
                    {
                        EXT_ID = ParseHelper.Parse<int>(row["EXT_ID"]),
                        EXT_CODE = ParseHelper.Parse<string>(row["EXT_CODE"]),
                        EXT_URL = ParseHelper.Parse<string>(row["EXT_URL"]),
                        PIC_NAME = ParseHelper.Parse<string>(row["PIC_NAME"]),
                        ITEAM_LIST = ParseHelper.Parse<string>(row["items_code"])
                        
                    };
                });
            return exttypeList;
        }
        //类型操作
        public bool EditExtType(Base_ExtTypeCond cond)
        {
            StringBuilder strSql = new StringBuilder();
            //action:0(删除)
            if (cond.action == 0)
            {
                List<DbCmdInfo> cmdlist = new List<DbCmdInfo>();
                strSql.Append("delete EXT_TYPE WHERE EXT_ID=:p_EXT_ID");
                DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString(),
                    delegate()
                    {
                        return DbCmdHelper.CreateParameters(dbProvider, "EXT_ID").ToArray();
                    });
                cmd.DbCmdInfo.CommandType = CommandType.Text;
                cmd.SetParamValues(cond.EXT_ID);
                cmdlist.Add(cmd.DbCmdInfo);

                DbCmdHelper cmd2 = new DbCmdHelper(dbProvider, "delete EXT_ITEM where EXT_ID=:p_EXT_ID",
                   delegate()
                   {
                       return DbCmdHelper.CreateParameters(dbProvider, "EXT_ID").ToArray();
                   });
                cmd2.DbCmdInfo.CommandType = CommandType.Text;
                cmd2.SetParamValues(cond.EXT_ID);
                cmdlist.Add(cmd2.DbCmdInfo);
                return DbManager.ExecTrans(dbProvider, cmdlist.ToArray());
            }
            return false;
        }
       /// <summary>
       /// 更新文件格式
       /// </summary>
       /// <param name="cond"></param>
       /// <returns></returns>
        public bool UpdateExtType(ExtTypeinfo cond)
        {
            StringBuilder strSql = new StringBuilder();
            List<DbCmdInfo> cmdlist = new List<DbCmdInfo>();
            strSql.Append("update EXT_TYPE set EXT_CODE=:p_EXT_CODE,EXT_URL=:p_EXT_URL WHERE EXT_ID=:p_EXT_ID ");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString(),
                delegate()
                {
                    return DbCmdHelper.CreateParameters(dbProvider, "EXT_CODE", "EXT_URL", "EXT_ID").ToArray();
                });
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            cmd.SetParamValues(cond.EXT_CODE, cond.EXT_URL, cond.EXT_ID);
            cmdlist.Add(cmd.DbCmdInfo);
            return DbManager.ExecTrans(dbProvider, cmdlist.ToArray());
        }
        /// <summary>
        /// 获取扩展名列表
        /// </summary>
        /// <param name="DICTTYPE_ID"></param>
        /// <param name="pageInfo"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public List<Base_ExtItemCond> QueryExtItemList(decimal EXT_ID, PageInfo pageInfo, OrderInfo order)
        {

            DbCmdHelper cmd = new DbCmdHelper(dbProvider, "sp_getpageextitemlist",
                   delegate()
                   {
                       return DbCmdHelper.CreateParameters(dbProvider, true, "EXT_ID").ToArray();
                   });
            cmd.SetParamValues(order, pageInfo, EXT_ID);
            List<Base_ExtItemCond> dictList = DbManager.GetPageInfos<Base_ExtItemCond>(cmd, pageInfo,
                delegate(out Base_ExtItemCond dict, DataRow row)
                {
                    dict = new Base_ExtItemCond
                    {
                        ITEM_ID = ParseHelper.Parse<decimal>(row["ITEM_ID"]),
                        EXT_ID = ParseHelper.Parse<decimal>(row["EXT_ID"]),
                        EXT_CODE = ParseHelper.Parse<string>(row["EXT_CODE"]),
                        ITEM_CODE = ParseHelper.Parse<string>(row["ITEM_CODE"])
                    };
                });
            return dictList;
        }

        /// <summary>
        ///  编辑扩展名元素
        /// </summary>
        /// <param name="cond"></param>
        /// <returns></returns>
        public bool EditExtItem(Base_ExtItemCond cond)
        {
            
            StringBuilder strSql = new StringBuilder();
            //action:0(删除)
            if (cond.action == 0)
            {

                strSql.Append("delete ext_item WHERE item_id=:p_ITEM_ID");
                DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString(),
                    delegate()
                    {
                        return DbCmdHelper.CreateParameters(dbProvider, "ITEM_ID").ToArray();
                    });
                cmd.DbCmdInfo.CommandType = CommandType.Text;
                cmd.SetParamValues(cond.ITEM_ID);
                return DbManager.ExecNonQuery(cmd) > 0;

            }
           
            else if (cond.action == 2)
            {
                Base_ExtItemCond itemcond = getItemByCode(cond.ITEM_CODE);
                if (itemcond == null)
                {
                    strSql.Append("INSERT INTO ext_item ( EXT_ID,ITEM_CODE");
                    strSql.Append(") VALUES (:p_EXT_ID,:p_ITEM_CODE)");
                    DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString(),
                        delegate()
                        {
                            return DbCmdHelper.CreateParameters(dbProvider, "EXT_ID", "ITEM_CODE").ToArray();
                        });
                    cmd.DbCmdInfo.CommandType = CommandType.Text;
                    cmd.SetParamValues(cond.EXT_ID, cond.ITEM_CODE.Trim());
                    return DbManager.ExecNonQuery(cmd) > 0;
                }
            }

            return false;
        }
       /// <summary>
       /// 新增文件格式
       /// </summary>
       /// <param name="cond"></param>
       /// <returns></returns>
        public bool ExtTypeAdd(ExtTypeinfo cond)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@" insert into EXT_TYPE(EXT_CODE, EXT_URL, PIC_NAME)
                           values(:p_EXT_CODE, :p_EXT_URL, :p_PIC_NAME)");


            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString(),
                delegate
                {
                    return DbCmdHelper.CreateParameters(dbProvider, "EXT_CODE", "EXT_URL", "PIC_NAME").ToArray();
                });

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            cmd.SetParamValues(cond.EXT_CODE, cond.EXT_URL, cond.PIC_NAME);
            return DbManager.ExecNonQuery(cmd) > 0;
        }
        /// <summary>
        /// 获取扩展名列表
        /// </summary>
        public DataTable QueryExtIList()
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select ext_code from ext_type");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;

        }


    }
}
